package com.itstu.gulimall.seckill.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.itstu.common.utils.R;
import com.itstu.gulimall.seckill.feign.CouponFeignService;
import com.itstu.gulimall.seckill.feign.ProductFeignService;
import com.itstu.gulimall.seckill.service.SeckillService;
import com.itstu.gulimall.seckill.to.SecKillSkuRedisTo;
import com.itstu.gulimall.seckill.vo.SeckillSessionsWithSkus;
import com.itstu.gulimall.seckill.vo.SkuInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 *
 * 商品的定时上架：
 *    每天晚上3点上架最近三天需要秒杀的商品
 * @author shhjiang_001
 * @create 2020-06-04 21:37
 */
@Service
@Slf4j
public class SeckillSkuScheduled {
    @Autowired
    SeckillService seckillService;
    @Autowired
    RedissonClient redissonClient;
    private final String UPLOAD_LOCK = "seckill:upload:lock";
    @Scheduled(cron = "0 0 * * * ?")
    public void uploadSeckillSkuLast3Days(){
        RLock lock = redissonClient.getLock(UPLOAD_LOCK);
        // 添加分布式锁
        lock.lock(10, TimeUnit.SECONDS);
        try {
            log.info("上架商品。。。。。");
            seckillService.uploadSeckillSkuLast3Days();
        }finally {
            lock.unlock();
        }
    }

}
